package com.jingding.procurement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jingding.procurement.enumeration.EntityEnum;
import com.jingding.procurement.mapper.SupplierInspectionSheetMapper;
import com.jingding.procurement.pojo.entity.Entity;
import com.jingding.procurement.pojo.entity.SupplierInspectionSheetEntity;
import com.jingding.procurement.pojo.vo.PurchasePoundsVO;
import com.jingding.procurement.pojo.vo.SupplierInspectionSheetVO;
import com.jingding.procurement.service.SupplierService;
import com.jingding.procurement.util.IO;
import com.jingding.procurement.util.Requesting;
import com.jingding.procurement.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

@Service
@Slf4j
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierInspectionSheetMapper mapper;

    @Value("${SISE_url}")
    private String SISE_url;
    @Override
    public JsonResult pushSupplierInspectionSheet(Entity entity, String sTime) throws IOException, ClassNotFoundException {
        List<SupplierInspectionSheetVO> supplierInspectionSheetVOS = mapper.selSupplierInspectionSheet(entity.getStartTime(), entity.getEndTime());
        if (supplierInspectionSheetVOS.size()<1){
            log.debug("暂无[供方检验单],下次任务开始时间不做变更,时间为:{} ,暂不推送数据  " , sTime);;
            return JsonResult.ok("暂无[供方检验单],下次任务开始时间不做变更 时间为:  " + sTime);
        }
        String jsonObject=null;
        int batchSize = 10;
        for (int i = 0; i < supplierInspectionSheetVOS.size(); i += batchSize) {
            List<SupplierInspectionSheetVO> batch = supplierInspectionSheetVOS.subList(i,
                    Math.min(i + batchSize, supplierInspectionSheetVOS.size()));
            jsonObject = Requesting.httpPost(SISE_url,
                    JSON.toJSONString(batch, SerializerFeature.WriteNullStringAsEmpty));
        }
        log.debug("[供方检验单]响应:{}",jsonObject);
        SupplierInspectionSheetEntity supplierInspectionSheetEntity = IO.supplierInspectionSheetRead();
        if (JSON.parseObject(jsonObject).getObject("code", (Type) String.class).equals("200")) {
            supplierInspectionSheetEntity.setStartTime(sTime);
            IO.supplierInspectionSheetWrite(supplierInspectionSheetEntity);
            log.debug("[供方检验单]起始时间修改完成:{}", sTime);
            log.debug("[供方检验单]条数:{} ,{}推送完成", supplierInspectionSheetVOS.size(),sTime);
            return JsonResult.ok("[供方检验单]数据推送完成,推送条数: "+supplierInspectionSheetVOS.size()+". 推送时间: "+sTime);
        }
        log.debug("[供方检验单]推送失败: {}",jsonObject);
        return JsonResult.fail(400, "[供方检验单]推送失败,下次任务开始时间为:  " + supplierInspectionSheetEntity.getStartTime()+"失败原因: "+jsonObject+"\\n" +
                "推送内容:"+JSON.toJSONString(supplierInspectionSheetVOS, SerializerFeature.WriteNullStringAsEmpty));

    }
}
